// pages/index/store_admin/store_edit.js
Page({

  data: {    printtype:[],url:'',url1:'',urllist:[],radio:'',name:'',phone:'',beizhu:'',id:'',urllist1:[],arr2:[],index2:0,
    //获取过来的店铺duixiang
    shoplist:{},mune:[],
    str:'',file:'',type:'',remark:'',radio3:'',address:'',lng:'',lat:'',radio4:''
  },

  onLoad: function (options) {
    this.getuserid()
    console.log(options);
    this.setData({type:options.type})
    if(options.type==2){
    let id=options.id
    this.setData({id})
    this.gettianjialist()
    this.getdianpulist(id)
    
    }else{
      let id=options.id
      this.setData({id})
      this.gettianjialist()
      this.getdianpulist1(id)
      
    }
  },
  onShow(){
    
   
  },
  gettianjialist(){
    wx.request({
      url: 'https://zujieba.com/Api/storeadminstore/type.html',
      success:(res)=>{
       console.log(res);
    let printtype=res.data.data
    
    this.setData({printtype})
     }
    })
  },
  toggle(e){
      let list=this.data.printtype
      let index=e.currentTarget.dataset.index
      list[index].checked=!list[index].checked
      console.log(list)
      this.setData({
        printtype:list
      })
      
  },
  toggle3(e){
    this.setData({
      radio3:e.currentTarget.dataset.radio3
    })
},toggle4(e){
  this.setData({
    radio4:e.currentTarget.dataset.radio
  })
},
  //上传照片
   upload(){
     wx.chooseImage({
       count:1,
       success:(res)=>{
         console.log(res)
       let url=  res.tempFiles[0].path
       wx.uploadFile({
        filePath: url,
        name: 'file',
        url: 'https://zujieba.com/index/upload/upload.html',
       success:(res1)=>{
         console.log(res1,1111);
         let data=JSON.parse(res1.data)
          this.setData({
            file:data.name
          })
         
       },
       fail:(res1)=>{
         console.log(res1,2);
         
       }
      })
    
      this.setData({
        url,
      
      })
       }
     })
   },
   upload1(){
     let urllist=this.data.urllist1
     
    
    
     
     
     
    wx.chooseImage({
      count:1,
      success:(res)=>{
        console.log(res)
      let url1=  res.tempFiles[0].path
      
      
     
      wx.uploadFile({
        filePath: res.tempFiles[0].path,
        name: 'file',
        url: 'https://zujieba.com/index/upload/upload.html',
        success:(res1)=>{
          let data=JSON.parse(res1.data)
          let url='https://zujieba.com/file_index/'+data.name
          urllist.push(url)
          this.setData({
            urllist1:urllist
          })
        }})
    }})
    
      
    
  },
 
 
   del(){
     this.setData({
       url:''
     })
   },
   del1(e){
    
     let list='urllist1'
     let urllist=this.data.urllist1
    
     urllist.splice(e.currentTarget.dataset.index,1)
     
     
    this.setData({
    [list]:urllist
    })
  },
  //选择是否配送
  toggle1(e){
    let radio='shoplist.tp'
     this.setData({
       [radio]:e.currentTarget.dataset.radio,
       radio:e.currentTarget.dataset.radio
     })
  },
  getdistance(e){
      this.setData({
        distance:e.detail.value
      })
  },
  getmoneyvalue(e){
    this.setData({
      money:e.detail.value
    })
  },
  change1(e){
    this.setData({
      name:e.detail.value
    })
  },
  change2(e){
    this.setData({
      phone:e.detail.value
    })
  },
  change3(e){
    this.setData({
      remark:e.detail.value
    })
  },
  //根据id获取店铺
  getdianpulist(id){
    wx.request({
      url: 'https://zujieba.com/Api/storeadminstore/edit_store.html',
      data:{id:id,type:2},
      method:'POST',
      success:(res)=>{
        console.log(res);
        let shoplist=res.data.data
        let urllist1=res.data.data.banner
        let arr6=[]
        urllist1.forEach((item)=>{
          let url='https://zujieba.com/file_index/'+item
           arr6.push(url)
        })
        let url=res.data.data.pic
      let file=  url.slice(31)
      console.log(file);
      
        //拿到获取到的打印类型数据
        let printtype=this.data.printtype
        let mune=res.data.data.mune
        printtype.forEach((item)=>{
          mune.forEach((item1)=>{
            if(item.id==item1){
              item.checked=true
            }
          })
        })

       
        
        
        
        this.setData({
          printtype,
          shoplist,
          mune,
          url,
          file,
          name:res.data.data.title,
          radio:res.data.data.tp,
          phone:res.data.data.phone,
          remark:res.data.data.remark,
          urllist1:arr6,
          radio3:res.data.data.package,
          radio4:res.data.data.express
        })
      }
    })
  },
 
   //修改店铺
   editclick1(){
     let urllist1=this.data.urllist1
     let arr7=[]
      urllist1.forEach((item)=>{
        let str=item.slice(31)
        arr7.push(str)
      })
      let string=arr7.join(',')
      console.log(string);
      
   
   
   
     //打印类型
    let mune=''
    this.data.printtype.forEach((item,index)=>{
        if(item.checked==true){
        /*  mune.push(String(item.id))*/
         mune=mune+","+item.id
          
        }
    })
    console.log(mune );
    let city,lng,lat
    if(this.data.address==''){
       city=this.data.shoplist.city
       lng=this.data.shoplist.lng
       lat=this.data.shoplist.lat
    }
    if(this.data.address!=''){
      city=this.data.address
      lng=this.data.lng
      lat=this.data.lat
    }
    console.log(this.data.name,this.data.file,city,lat,lng,this.data.phone,this.data.radio,this.data.distance,this.data.remark,this.data.radio4,this.data.id);
    
    
    wx.request({
      method:'POST',
      url: 'https://zujieba.com/Api/storeadminstore/edit_store.html',
      data:{title:this.data.name,pic:this.data.file,uid:wx.getStorageSync('uid'),city,lat,lng,phone:this.data.phone,tp:this.data.radio,range:this.data.distance,remark:this.data.remark,express:this.data.radio4,id:this.data.id,type:1,package:this.data.radio3,dema:this.data.money,
    banner:string,mune},
    success:(res)=>{
      console.log(res,mune);
      if(res.data.success==1){
        wx.showToast({
          title:'修改成功',
          duration:2000,
          icon:'success'
        })
        wx.navigateBack()
      }else{
        wx.showToast({
          title: res.data.tips,
          icon:'none'
        })
      }
      
    }
    })
   
    
  },
  
   //根据id获取店铺
   getdianpulist1(id){
     console.log(id);
     
    wx.request({
      url: 'https://zujieba.com/Api/shareradminstore/store_more.html',
      data:{id:id,pid:wx.getStorageSync('uid')},
      success:(res)=>{
        console.log(res);
        let shoplist=res.data.data
        let urllist1=res.data.data.banner
        let arr6=[]
        urllist1.forEach((item)=>{
          let url='https://zujieba.com/file_index/'+item
           arr6.push(url)
        })
        let url=res.data.data.pic
        let file=url.slice(31)
        console.log(file);
        
        //拿到获取到的打印类型数据
        let printtype=this.data.printtype
        let mune=res.data.data.mune
        printtype.forEach((item)=>{
          mune.forEach((item1)=>{
            if(item.id==item1){
              item.checked=true
            }
          })
        })

       
        
        
        
        this.setData({
          radio4:res.data.data.express,
          radio3:res.data.data.package,
          money:res.data.data.dema,
          name:res.data.data.title,
          phone:res.data.data.phone,
          radio:res.data.data.tp,
          distance:res.data.data.range,
          printtype,
          shoplist,
          mune,
          remark:res.data.data.remark,
          url,
          file,
          urllist1:arr6
        })
      }
    })
  },
 //共享商修改店铺
  editclick11(){
    let urllist1=this.data.urllist1
    let arr7=[]
     urllist1.forEach((item)=>{
       let str=item.slice(31)
       arr7.push(str)
     })
     let string=arr7.join(',')
     console.log(string);
     
     console.log(this.data.name);
  
  
    //打印类型
   let mune=''
   this.data.printtype.forEach((item,index)=>{
       if(item.checked==true){
       /*  mune.push(String(item.id))*/
        mune=mune+","+item.id
         
       }
   })

   let city,lng,lat
   if(this.data.address==''){
      city=this.data.shoplist.city
      lng=this.data.shoplist.lng
      lat=this.data.shoplist.lat
   }
   if(this.data.address!=''){
     city=this.data.address
     lng=this.data.lng
     lat=this.data.lat
   }
   console.log(mune );
   console.log(this.data.file,string,wx.getStorageSync('uid'),this.data.radio,this.data.distance,this.data.id,this.data.name,this.data.phone,this.data.remark);
    
   
   wx.request({
     url: 'https://zujieba.com/Api/shareradminstore/edit_store.html',
     data:{title:this.data.name,pic:this.data.file,pid:wx.getStorageSync('uid'),city,lat,lng,phone:this.data.phone,tp:this.data.radio,range:this.data.distance,id:this.data.id,remark:this.data.remark,uid:this.data.arr3[this.data.index2].id,dema:this.data.money,package:this.data.radio3,express:this.data.radio4,
   banner:string,mune},
   success:(res)=>{
     console.log(res);
     if(res.data.success==1){
       wx.showToast({
         title:res.data.tips,
         duration:2000,
         icon:'success'
       })
       wx.navigateBack()
     }else{
      wx.showToast({
        title: res.data.tips,
        icon:'none'
      })
    }
     
   }
   })
  
   
 },
 //获取共享商用户列表id
 getuserid(){
  wx.request({
    url: 'https://zujieba.com/Api/shareradminstore/member.html',
    data:{pid:wx.getStorageSync('uid')},
    success:(res)=>{
        console.log(res);
       let arr2= res.data.data
       let arr=[]
       arr2.forEach((item)=>{
           arr.push(item.name)
       })

       this.setData({
         arr3:arr2,
         arr2:arr
       })
        
    }
  })
},
 getlocation(){
   wx.chooseLocation({
      success:(res)=>{
        this.setData({
          address:res.address,
          lng:res.longitude,
          lat:res.latitude
        })

      }
   })
 },
 saveimage(){
    wx.getImageInfo({
      src: this.data.shoplist.store_code,
      success:(res)=>{
        console.log(res);
        wx.saveImageToPhotosAlbum({
          filePath: res.path,
          success:(res1)=>{
            console.log(res1);
            
          }
        })
        
      }
    })
 }
  
  
})